<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\constant\Constant;
use app\common\model\Information;
use app\common\model\Match;
use app\common\model\User;
use think\Exception;

class PublicManager extends BaseManager
{

    /**
     * 根据用户获取相关赛事权限
     * @param User $user
     * @param $matchId
     * @param $rule
     * @return array
     */
    public static function getPr(User $user,$matchId,$rule=Match::ROUND)
    {
        try {
            $purview = [];
            $referee = \app\common\model\Referee::where('match_id',$matchId)->where('user_id',$user->id)->find();
            if ($referee){
                $purview = explode(',',$referee['purview']);
            }
            if (!in_array($rule,$purview)){
                throw new Exception('当前无权限操作');
            }
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($purview);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    /**
     * 获取资讯列表
     * @param $page
     * @param $status
     * @param $step
     * @return array
     */
    public static function information($page,$status=Information::STATUS_INIT,$step=Constant::PAGE_LIMIT)
    {
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        if ($page < 1){
            $page = 1;
        }
        try {
            $start = ($page - 1) * $step;
            $count = Information::where('status',$status)->count();
            $info = Information::where('status',$status)->order('sort asc')->limit($start,$step)->select();
            if($info){
                foreach ($info as $k=>&$v){
                    if ($v->image && strrpos($v->image,'http')===false){
                        $v->image = cdnurl($v->image);
                    }
                }
            }
            $return['list'] = $info;
            $pageData = page($count,$step,$page);
            $data = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($data);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

}